<xsl:stylesheet xmlns:marc="http://www.loc.gov/MARC21/slim" 
                xmlns:oai="http://www.openarchives.org/OAI/2.0/" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                version="1.0" exclude-result-prefixes="marc oai">
    <xsl:output method="xml" indent="yes" encoding="UTF-8"/>
    <xsl:template match="/">
        <update>
	<add>
            <xsl:for-each select="//marc:record" >
                <doc>
                    <field name="source">invenio</field>
                    <field name="identifier">
                        <xsl:value-of select="../../oai:header/oai:identifier"/>
                    </field>
                    <field name="id">
                        <xsl:value-of select="marc:controlfield[@tag=001]"/>
                    </field>
                    <field name="format">
                        <xsl:value-of select="./marc:controlfield[@tag=007]"/>
                    </field>
                    <field name="language">
                        <xsl:value-of select="substring(./marc:controlfield[@tag=008], 36, 3)"/>
                    </field>
                    <xsl:if test="marc:datafield[@tag='773' and @ind1='1']/marc:subfield[@code='x']">
                        <field name="isbn">
                            <xsl:value-of select="marc:datafield[@tag='773' and @ind1='1']/marc:subfield[@code='x']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag='773' and @ind1='1']/marc:subfield[@code='z']">
                        <field name="issn">
                            <xsl:value-of select="marc:datafield[@tag='773' and @ind1='1']/marc:subfield[@code='z']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=520]/marc:subfield[@code='a']">
                        <field name="annotace">
                            <xsl:for-each select="marc:datafield[@tag=520]/marc:subfield[@code='a']">
                            <xsl:value-of select="."/>&#160;
                            </xsl:for-each>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag='711' and @ind1='2']/marc:subfield[@code='a']">
                        <field name="konference">
                            <xsl:value-of select="marc:datafield[@tag='711' and @ind1='2']/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag='711' and @ind1='2']/marc:subfield[@code='g']">
                        <field name="konference">
                            <xsl:value-of select="marc:datafield[@tag='711' and @ind1='2']/marc:subfield[@code='g']"/>
                        </field>
                    </xsl:if>
                    <xsl:choose>
                        <xsl:when test="marc:datafield[@tag=090]">
                            <field name="callnumber">
                                <xsl:value-of select="marc:datafield[@tag=090]/marc:subfield[@code='a']"/>
                                <xsl:value-of select="marc:datafield[@tag='090']/marc:subfield[@code='b']"/>
                            </field>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:if test="marc:datafield[@tag=050]">
                                <field name="callnumber">
                                    <xsl:value-of select="marc:datafield[@tag=050]/marc:subfield[@code='a']"/>
                                    <xsl:value-of select="marc:datafield[@tag='050']/marc:subfield[@code='b']"/>
                                </field>
                            </xsl:if>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:if test="marc:datafield[@tag=100]/marc:subfield[@code='a']">
                        <field name="author">
                            <xsl:value-of select="marc:datafield[@tag=100]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=110]/marc:subfield[@code='a']">
                        <field name="author">
                            <xsl:value-of select="marc:datafield[@tag=110]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=111]/marc:subfield[@code='a']">
                        <field name="author">
                            <xsl:value-of select="marc:datafield[@tag=111]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <field name="title">
                        <xsl:value-of select="marc:datafield[@tag=245]/marc:subfield[@code='a']"/>
                        <xsl:text>&#160;</xsl:text>
                        <xsl:value-of select="marc:datafield[@tag='245']/marc:subfield[@code='b']"/>
                    </field>
                    <xsl:for-each select="marc:datafield[@tag=246]">
                        <field name="title2">
                            <xsl:value-of select="./marc:subfield[@code='a']"/>
                        </field>
                    </xsl:for-each>
                    <xsl:if test="marc:datafield[@tag=240]/marc:subfield[@code='a']">
                        <field name="title2">
                            <xsl:value-of select="marc:datafield[@tag=240]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=130]/marc:subfield[@code='a']">
                        <field name="title2">
                            <xsl:value-of select="marc:datafield[@tag=130]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=260]/marc:subfield[@code='b']">
                        <field name="publisher">
                            <xsl:value-of select="marc:datafield[@tag=260]/marc:subfield[@code='b']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=260]/marc:subfield[@code='c']">
                        <field name="publishDate">
                            <xsl:value-of select="marc:datafield[@tag=260]/marc:subfield[@code='c']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=300]/marc:subfield[@code='b']">
                        <field name="physical">
                            <xsl:value-of select="marc:datafield[@tag=300]/marc:subfield[@code='b']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=362]/marc:subfield[@code='a']">
                        <field name="dateSpan">
                            <xsl:value-of select="marc:datafield[@tag=362]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=440]/marc:subfield[@code='a']">
                        <field name="series">
                            <xsl:value-of select="marc:datafield[@tag=440]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=505]/marc:subfield[@code='a']">
                        <field name="contents">
                            <xsl:value-of select="marc:datafield[@tag=505]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=505]/marc:subfield[@code='t']">
                        <field name="contents">
                            <xsl:for-each select="marc:datafield[@tag=505]/marc:subfield[@code='t']">
                                <xsl:value-of select="."/>
                                <xsl:text></xsl:text>
                            </xsl:for-each>
                        </field>
                    </xsl:if>
                    <xsl:call-template name="subjects"/>
                    
        <xsl:if test="marc:datafield[@tag=650 and @ind2='7']/marc:subfield[@code='2']/text()='PSH'">
            <field name="has_psh">true</field>
            
            <xsl:for-each select="marc:datafield[@tag=650 and @ind2='7']/marc:subfield[@code='0']">
                <field name="psh_key">
                    <xsl:value-of select="."/>
                </field>
            </xsl:for-each>
        </xsl:if>
                    <xsl:for-each select="marc:datafield[@tag=700]">
                        <field name="author2">
                            <xsl:value-of select="./marc:subfield[@code='a']"/>
                        </field>
                    </xsl:for-each>
                    <xsl:if test="marc:datafield[@tag=780]/marc:subfield[@code='a']">
                        <field name="oldTitle">
                            <xsl:value-of select="marc:datafield[@tag=780]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=785]/marc:subfield[@code='a']">
                        <field name="newTitle">
                            <xsl:value-of select="marc:datafield[@tag=785]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:if test="marc:datafield[@tag=830]/marc:subfield[@code='a']">
                        <field name="series">
                            <xsl:value-of select="marc:datafield[@tag=830]/marc:subfield[@code='a']"/>
                        </field>
                    </xsl:if>
                    <xsl:for-each select="marc:datafield[@tag=856]/marc:subfield[@code='u']">
                        <field name="url">
                            <xsl:value-of select="."/>
                        </field>
                    </xsl:for-each>
                    <xsl:for-each select="marc:datafield[@tag=998]/marc:subfield[@code='a']">
                        <field name="institution">
                            <xsl:value-of select="."/>
                        </field>
                    </xsl:for-each>
                </doc>
            </xsl:for-each>
        </add>
	<xsl:for-each select="//oai:record/oai:header[@status='deleted']">
		<delete><query>identifier:"<xsl:value-of select="oai:identifier"/>"</query></delete>
	</xsl:for-each>
	</update>
    </xsl:template>



    <xsl:template name="subjects">
        <xsl:for-each select="marc:datafield[@tag=600]">
            <xsl:if test="./marc:subfield[@code='a']/text()">
                <field name="subject">
                    <xsl:value-of select="./marc:subfield[@code='a']"/>
                </field>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=610]">
            <xsl:if test="./marc:subfield[@code='a']/text()">
                <field name="subject">
                    <xsl:value-of select="./marc:subfield[@code='a']"/>
                </field>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=630]">
            <xsl:if test="./marc:subfield[@code='a']/text()">
                <field name="subject">
                    <xsl:value-of select="./marc:subfield[@code='a']"/>
                </field>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=650]">
            <xsl:if test=".[@ind2='7']/marc:subfield[@code='2']/text()!='PSH'">
                <xsl:if test="./marc:subfield[@code='a']/text()"><field name="subject">
                        <xsl:value-of select="./marc:subfield[@code='a']"/>
                    </field>
                </xsl:if>
                <xsl:if test="./marc:subfield[@code='x']/text()">
                    <field name="subject">
                        <xsl:value-of select="./marc:subfield[@code='x']"/>
                    </field>
                </xsl:if>
            </xsl:if>
        </xsl:for-each>
        
        <xsl:for-each select="marc:datafield[@tag=651]">
            <xsl:if test="./marc:subfield[@code='a']/text()">
                <field name="subject">
                    <xsl:value-of select="./marc:subfield[@code='a']"/>
                </field>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=655]">
            <xsl:if test="./marc:subfield[@code='a']/text()">
                <field name="subject">
                    <xsl:value-of select="./marc:subfield[@code='a']"/>
                </field>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="marc:datafield[@tag=653 and (@ind1=' ' or @ind1='0')]">
            <xsl:if test="./marc:subfield[@code='a']/text()">
                <field name="subject">
                    <xsl:value-of select="./marc:subfield[@code='a']"/>
                </field>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
